package tq.com.tqteacher.model;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import tq.com.tqcom.net.INetCallback;
import tq.com.tqteacher.activity.newPublish.TQVideoSetsManager;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.propertyEnum.TQModify;

public class TQWKPublish extends TQNewPublish {
    private ArrayList<TQAtachModel> mVideoSetsModels = new ArrayList<>();

    public void addAttach(TQAtachModel pTQAtachModel){
        mVideoSetsModels.add(pTQAtachModel);
        mkDescFromAttach();
    }

    public void delAttach(TQAtachModel pModel){
        mVideoSetsModels.remove(pModel);
        mkDescFromAttach();
    }
    public ArrayList<TQAtachModel> getVideoSetsModels() {
        return mVideoSetsModels;
    }

    @Override
    public void fromJson(JSONObject pObject) {
        super.fromJson(pObject);

        if(pObject == null)return;

        JSONArray lArray = pObject.optJSONArray("desc_ext");
        if(lArray != null){
            StringBuffer lBuffer = new StringBuffer();
            for (int i = 0; i < lArray.length(); i++){
                String lObject = lArray.optString(i);
                if(lObject == null) continue;

                if(lBuffer.length() > 0){
                    lBuffer.append("\n");
                }
                lBuffer.append(lObject);
            }

            if(lBuffer.length() > 0){
                setDesc(lBuffer.toString());
            }
        }

    }

    @Override
    public void onPulishComplete(INetCallback pNetCallback) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("category", Integer.valueOf(getType()));
        param.put("title", getName());
        param.put("grade", Integer.valueOf(getGrade()));
        param.put("chapter", Integer.valueOf(getParentId()));
        param.put("access_type", Integer.valueOf(getShow()));

        TQNetInterface.newPublish(param, pNetCallback);
    }

    @Override
    public void onModifyBaseInfo(INetCallback pNetCallback) {
        HashMap<TQModify.MaterialBaseInfo, Object> param = new HashMap<>();
        param.put(TQModify.MaterialBaseInfo.title, getName());
        param.put(TQModify.MaterialBaseInfo.access_type, String.valueOf(getShow()));
        param.put(TQModify.MaterialBaseInfo.grade, String.valueOf(getGrade()));

        TQNetInterface.modifyMaterialInfo(getSeq_id(), param, pNetCallback);
    }

    public void refreshAttach(JSONArray pObject){
        if(pObject == null)return;
        mVideoSetsModels.clear();

        for (int i = 0; i < pObject.length(); i++){
            try {
                JSONObject lObject = pObject.getJSONObject(i);
                TQAtachModel lAtachModel = new TQAtachModel();
                lAtachModel.fromJson(lObject);

                mVideoSetsModels.add(lAtachModel);
            }catch (JSONException pE){
                pE.printStackTrace();
            }
        }

        mkDescFromAttach();
    }

    private void mkDescFromAttach(){
        if(mVideoSetsModels.size() == 0){
            setDesc("");
            return;
        }

        StringBuffer lBuffer = new StringBuffer();
        for (TQAtachModel lModel:
             getVideoSetsModels()) {
            if(lBuffer.length() > 0){
                lBuffer.append('\n');
            }

            lBuffer.append(lModel.getDesc());
        }
        if(lBuffer.length() > 0){
            setDesc(lBuffer.toString());
        }
    }
}
